package com.itita.weibo.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.manager.WeiboManager;
import com.itita.weibo.model.MoreOperAdapter;
import com.itita.weibo.util.WeiboUtil;

public final class MoreActivity extends Activity implements IWeiboActivity{
	private ListView listView;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//取消标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.moreitems);
		
		initControlsAndAddControlsEvent();
	
	}
	
	@Override
	public void initControlsAndAddControlsEvent() {
		this.listView = (ListView) this.findViewById(R.id. moreItemsListView);
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(R.string.accountMana);
		list.add(R.string.clearCache);
//		list.add(R.string.authorizeItitaWeibo);
		list.add(R.string.suggestionToDeveloper);
		list.add(R.string.about);
		this.listView.setAdapter(new MoreOperAdapter(this, list));
		
		this.listView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if(position == 0){ //账号管理
					Intent intent = new Intent(MoreActivity.this, AccountManagerActivity.class);
					startActivity(intent);
				}else if(position == 1){
//					Toast.makeText(MoreActivity.this, "清除缓存", Toast.LENGTH_SHORT).show();
				    AlertDialog.Builder  aBuilder = new AlertDialog.Builder(MoreActivity.this);
				    aBuilder.setMessage("您确定要清除缓存吗?");
				    aBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							ProgressDialog	pd = new ProgressDialog(MoreActivity.this);
							pd.setMessage("正在清除缓存 请稍后...");
							pd.show();
							new WeiboManager().clearCache(MoreActivity.this); 
							pd.dismiss();
							WeiboUtil.myToastShow(MoreActivity.this, "清除成功");
						}//
					});
				    aBuilder.setNegativeButton("取消", null);
				    
				    aBuilder.show(); 
				}else if(position == 2){ //意见反馈
					Intent intent  = new Intent(MoreActivity.this, SendSuggestionToAuthorActivity.class);
					MoreActivity.this.startActivity(intent);
//					Toast.makeText(MoreActivity.this, "itita微博", Toast.LENGTH_SHORT).show();
				    
				}else if(position == 3){
					Intent intent  = new Intent(MoreActivity.this, AboutActivity.class);
					MoreActivity.this.startActivity(intent); 
				} 
				
			}
		});
	}
	
	public void init() {
		
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Log.d("keydown", ".................."+keyCode);
		if(keyCode==4)//如果用户按下了返回键
		{
			MainService.promptExitApp(this);
		}
		return true;
	}
	 


	 
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		
	}

}
